package com.chuangjiangx.member.common.utils;

import com.alibaba.fastjson.JSON;
import com.chuangjiangx.member.common.utils.AIFace.*;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AIMethodUtils {

    /**
     * RESTTemplate形式的新增人脸 AI 2.0
     * @param request
     * @return
     * @throws Exception
     */
    public static AIBaseResponse inputFace(AIFaceInputRequest request) throws Exception {
        JavaType javaType = TypeFactory.defaultInstance().constructParametricType(AIBaseResponse.class, AIFaceInputResponse.class);
        RequestUtils.ReqResponse<AIBaseResponse> reqResponse = RequestUtils.postForObject(AIConstant.AiFaceUrl.INPUT_FACE, request, javaType);
        if (!reqResponse.isSuccess()) {
            log.error("新增人脸失败:{}", JSON.toJSONString(reqResponse));
            throw new AiException("新增人脸失败");
        } else {
            return reqResponse.getData();
        }
    }

    /**
     * 更新人脸 AI 2.0
     * @param request
     * @return
     * @throws AiException
     */
    public static AIBaseResponse updateFace(UpdateFaceInputRequest request) throws AiException {
        JavaType javaType = TypeFactory.defaultInstance().constructParametricType(AIBaseResponse.class, UpdateFaceInputResponse.class);
        RequestUtils.ReqResponse<AIBaseResponse> reqResponse = RequestUtils.postForObject(AIConstant.AiFaceUrl.UPDATE_FACE, request, javaType);
        if (!reqResponse.isSuccess()) {
            log.error("更新人脸失败:{}", JSON.toJSONString(reqResponse));
            throw new AiException("更新人脸失败");
        } else {
            return reqResponse.getData();
        }
    }


    /**
     * 人脸预处理 AI2.0
     * @param request
     * @return
     * @throws AiException
     */
    public static AIBaseResponse perInputFace(AIAddFacePerProcessRequest request) throws AiException {
        JavaType javaType = TypeFactory.defaultInstance().constructParametricType(AIBaseResponse.class, AIAddFacePerProcessResponse.class);
        RequestUtils.ReqResponse<AIBaseResponse> reqResponse = RequestUtils.postForObject(AIConstant.AiFaceUrl.PRE_INPUT_FACE, request, javaType);
        if (!reqResponse.isSuccess()) {
            log.error("预处理人脸失败:{}", JSON.toJSONString(reqResponse));
            throw new AiException("预处理人脸失败");
        } else {
            return reqResponse.getData();
        }
    }


}
